<template>
  <transition name="fade">
  <div class="confirm-wrap" v-if="visible">
    <div class="confirm">
      <div class="hd">
        {{ title }}
        <!-- <i class="btn-close iconfont icon-close" @click="handleAction('close')"></i> -->
      </div>
      <div class="bd">
        <!-- <i v-if="type!=''" class="icon-type iconfont" :class="'icon-'+type"></i> -->
        {{ content }}
      </div>
      <div class="ft">
        <a class="btn border-right" @click="handleAction('cancel')">{{ cancelBtnText }}</a>
        <a class="btn" @click="handleAction('yes')">{{ yesBtnText }}</a>
      </div>
    </div>
    <div class="backdrop" @click="handleAction('close')"></div>
  </div>
  </transition>
</template>

<script>
  export default {
    name: "Confirm",
    data() {
      return {
        visible: false,
        title: '',
        content: '',
        yesBtnText: '确定',
        cancelBtnText: '取消',
        // type: '',
        promiseStatus: null
      }
    },
    watch:{
      // visible:function(curVal) {
      //   if(curVal&&document.body.scrollHeight > window.innerHeight){
      //     $('body').addClass('backdrop-open');
      //   }else{
      //     $('body').removeClass('backdrop-open');
      //   }
      // }
    },
    methods: {
      confirm() {
        this.visible = true
        return new Promise((resolve, reject) => {
          this.promiseStatus = { resolve, reject };
        });
      },
      handleAction(action){
        this.visible = false
        if(action == 'yes'){
          this.promiseStatus && this.promiseStatus.resolve();
        }else{
          this.promiseStatus && this.promiseStatus.reject();
        }
      }
    }
  }
</script>

<style lang="scss">
  @import '@/assets/css/base.scss';
  .confirm-wrap {
    position: fixed;
    top: 0;
    width: 100%;
    height: 100vh;
    .confirm {
      position: absolute;
      @include center;
      width: 4.4rem;
      height: 2.2rem;
      background: #fff;
      border-radius: 5px;
      overflow: hidden;
      z-index: 3;
      .hd {
        padding: 0.2rem;
        text-align: center;
        font-size: 0.28rem;
      }
      .bd {
        padding: 0 0.2rem 0.2rem 0.2rem;
        text-align: center;
        font-size: 0.32rem;
        border-bottom: 1px solid $bgGray;
      }
      .ft {
        display: flex;
        .btn {
          display: block;
          flex: 1;
          height: 0.8rem;
          line-height: 0.8rem;
          text-align: center;
          font-size: 0.28rem;
          color: #000000;
          &.border-right {
            border-right: 1px solid $bgGray;
          }
        }
      }
    }
    .backdrop {
      position: absolute;
      top: 0;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.5);
      z-index: 1;
    }
  }
  .fade-enter, .fade-leave-to {
    opacity: 0;
  }
  .fade-enter-active, .fade-leave-active {
    transition: opacity 0.2s linear;
  }
</style>